package com.my.fss.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.lang3.StringUtils;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 韩晗		2014-01-12
 */
@SuppressWarnings("serial")
public class StrUtil implements java.io.Serializable{


	private static final int COORDINATE_SCALE = 10;   //精度

	/**
	 * 清除字符串前后以及中间的所有空格
	 * @param str
	 */
	public static String trimAll(String str){
		if(str==null || str.trim().equals("")) {
			return "";
		}

		while (str.indexOf(" ") != -1) {
			str = str.substring(0, str.indexOf(" ")) + str.substring(str.indexOf(" ") + 1);
		}
		return str;
	}


	/**
	 * 判断字符串是否为空
	 * @param str
	 */
	public static boolean isNull(String str) {
		boolean flag = false;
		if(str==null || str.equals("") || ("undefined").equals(str.toLowerCase()) || "null".equals(str.toLowerCase())){
			flag = true;
		}
		return flag;
	}


	/**
	 * 将textarea文本变成html格式
	 */
	public static String textarea2Html(String str) {
		if (isNull(str)) {

		}else {
			str = str.replaceAll(" ", "&nbsp;");
			str = str.replaceAll("<", "&lt;");
			str = str.replaceAll(">", "&gt;");
			str = str.replaceAll("\r\n", "<br/>");
			str = str.replaceAll("\r", "<br/>");
			str = str.replaceAll("\n", "<br/>");
		}

		return str;
	}

	/**
	 * 将textarea文本变成html格式
	 */
	public static String textarea2AllHtml(String str) {
		if (isNull(str)) {

		}else {
			str = str.replaceAll("&", "&amp;");
			str = str.replaceAll("\"", "&quot;");
			str = str.replaceAll("￠", "&cent;");
			str = str.replaceAll("£", "&pound;");
			str = str.replaceAll("¥", "&yen;");
			str = str.replaceAll("€", "&euro;");
			str = str.replaceAll("§", "&sect;");
			str = str.replaceAll("©", "&copy;");
			str = str.replaceAll("®", "&reg;");
			str = str.replaceAll("™", "&trade;");
			str = str.replaceAll("×", "&times;");
			str = str.replaceAll("÷", "&divide;");
			str = str.replaceAll("<", "&lt;");
			str = str.replaceAll(">", "&gt;");
		}

		return str;
	}

	/**
	 * 将textarea文本换行改为空格
	 */
	public static String textarea2SimpleText(String str) {
		if (null == str || "".equals(str)) {
			return str;
		}
		str = str.replaceAll("\r\n", " ");
		str = str.replaceAll("\r", " ");
		str = str.replaceAll("\n", " ");
		str = str.replaceAll("\t", " ");

		return str;
	}

	/**
	 * 判断内容是否含有非法字符
	 *
	 * @author 王国庆
	 */
	public static boolean validateBody(String str){
		boolean isSuc = true;

		Matcher mj = Pattern.compile("<script(.*?)/script>").matcher(str.toLowerCase());
		Matcher mf = Pattern.compile("<iframe(.*?)/iframe>").matcher(str.toLowerCase());
		Matcher ms = Pattern.compile("<style(.*?)/style>").matcher(str.toLowerCase());
		Matcher mj1 = Pattern.compile("<script(.*?)/>").matcher(str.toLowerCase());
		Matcher mf1 = Pattern.compile("<iframe(.*?)/>").matcher(str.toLowerCase());
		Matcher ms1 = Pattern.compile("<style(.*?)/>").matcher(str.toLowerCase());

		if (mj.find()||mf.find()||ms.find()||mj1.find()||mf1.find()||ms1.find()) {
			isSuc = false;
		}

		return isSuc;
	}

	/**
	 * 过滤危险单词
	 *
	 * @author 王国庆
	 */
	public static String filterWord(String str ,String word){

		int le = 0;
		if (word.split(",").length > 0) {
			for (int i = 0; i < word.split(",").length; i++) {
				if (str.contains(word.split(",")[i])) {
					le++;
					str = str.replaceAll(word.split(",")[i], "*****");
				}
			}
		}

		str = str.replaceAll("onabort", "ｏnabort");
		str = str.replaceAll("onblur", "ｏnblur");
		str = str.replaceAll("onchange", "ｏnchange");
		str = str.replaceAll("onclick", "ｏnclick");
		str = str.replaceAll("ondblclick", "ｏndblclick");
		str = str.replaceAll("onerror", "ｏnerror");
		str = str.replaceAll("onfocus", "ｏnfocus");
		str = str.replaceAll("onkeydown", "ｏnkeydown");
		str = str.replaceAll("onkeypress", "ｏnkeypress");
		str = str.replaceAll("onkeyup", "ｏnkeyup");
		str = str.replaceAll("onload", "ｏnload");
		str = str.replaceAll("onmousedown", "ｏnmousedown");
		str = str.replaceAll("onmousemove", "ｏnmousemove");
		str = str.replaceAll("onmouseout", "ｏnmouseout");
		str = str.replaceAll("onmouseover", "ｏnmouseover");
		str = str.replaceAll("onmouseup", "ｏnmouseup");
		str = str.replaceAll("onreset", "ｏnreset");
		str = str.replaceAll("onresize", "ｏnresize");
		str = str.replaceAll("onselect", "ｏnselect");
		str = str.replaceAll("onsubmit", "ｏnsubmit");
		str = str.replaceAll("onunload", "ｏnunload");
		str = str.replaceAll("altKey", "ａltKey");
		str = str.replaceAll("button", "ｂutton");
		str = str.replaceAll("clientX", "ｃlientX");
		str = str.replaceAll("clientY", "ｃlientY");
		str = str.replaceAll("ctrlKey", "ｃtrlKey");
		str = str.replaceAll("metaKey", "ｍetaKey");
		str = str.replaceAll("relatedTarget", "ｒelatedTarget");
		str = str.replaceAll("screenX", "ｓcreenX");
		str = str.replaceAll("screenY", "ｓcreenY");
		str = str.replaceAll("shiftKey", "ｓhiftKey");
		str = str.replaceAll("href", "ｈref");


		if (le > 10) {
			str = "";
		}

		return str;
	}


	/**
	 * 去掉右空格，再剪切字符串
	 * @param str			源字符串
	 * @param n				要裁切的字符串长度
	 */
	public static String cutStr(String str, int n) {

		if (str==null || "null".equals(str.toLowerCase())) {
			return "";
		}

		while(!str.equals("") && str.charAt(str.length()-1)==' ') {
			str = str.substring(0, str.length()-1);
		}

		if (str.length() > n) {
			return str.substring(0, n) + "…";
		}

		return str;
	}


	/**
	 * 传入一个double值，返回小数点后两位的值，不足两位用零占位
	 */
	public static String doubleFormat(double d) {
		java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance();
		formatter.setMinimumFractionDigits(2);
		formatter.setMaximumFractionDigits(2);

		return formatter.format(d);
	}


	/**
	 * 传入一个double值，返回小数点后n位的值，不足n位用零占位
	 */
	public static String doubleFormat(double d, int n) {
		java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance();
		formatter.setMinimumFractionDigits(n);
		formatter.setMaximumFractionDigits(n);

		return formatter.format(d);
	}


	/**
	 * 将[id1][id2][id3]转成id1,id2,id3
	 * @param ids:[id1][id2][id3]
	 */
	public static String commaIds(String ids) {
		if (isNull(ids)) {
			return "";
		}
		else {
			ids = ids.trim().replaceAll("\\]\\[", ",").replaceFirst("\\[", "").replaceFirst("\\]", "");
			return ids;
		}
	}


	/**
	 * 获取下一个孩子的数字编号code值
	 * 例如：父Code=001 其最大编号的儿子是001006，则num=3,返回001007
	 * @param pCode 	父亲的Code
	 * @param cCode 	孩子中最大的Code
	 * @return		  	返回下一个Code值
	 */
	public static String getNextCode(String pCode, String cCode) {
		// 初始化，nextCode等于父Code
		String nextCode = pCode;
		// 获得编号分段长度
		int num = pCode.length() - cCode.length();

		// 将编号转为数字
		int tempCode = 1;
		if(!isNull(cCode)) {
			cCode = cCode.substring(cCode.length() - num);
			tempCode = Integer.valueOf(cCode)+1;
		}

		// 如果+1后溢出边界，则-1改回边界值
		if(String.valueOf(tempCode).length() > num) {
			tempCode = tempCode-1;
			nextCode = nextCode + tempCode;
		}
		// 补0
		else{
			for(int i=String.valueOf(tempCode).length(); i<num; i++) {
				nextCode += "0";
			}
			nextCode = nextCode + tempCode;
		}

		return nextCode;
	}


	/**
	 * 用0为ID补齐长度，并返回字符串
	 * @param id		ID值
	 * @param length	补齐到的长度
	 */
	public static String getStrId(Integer id, int length) {
		String str = id.toString();

		if(str.length() == length) {
			for(int i=0; i<length; i++) {
				str += "0";
			}
		}
		else{
			String str1 = "";
			for(int i=str.length(); i<length; i++) {
				str1 += "0";
			}
			str = str1 + str;
		}

		return str;
	}


	/**
	 * 验证邮箱地址是否正确
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email) {
		boolean flag = false;
		try{
			String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
			Pattern regex = Pattern.compile(check);
			Matcher matcher = regex.matcher(email);
			flag = matcher.matches();
		} catch(Exception e){
			flag = false;
		}
		return flag;
	}


	/**
	 * 验证手机号码
	 * @param mobile
	 * @return
	 */
	public static boolean isMobileNO(String mobile) {
		boolean flag = false;
		try{
			Pattern p = Pattern.compile("^((13[0-9])|(15[0-9])|(18[0-9]))\\d{8}$");
			Matcher m = p.matcher(mobile);
			flag = m.matches();
		} catch(Exception e){
			flag = false;
		}
		return flag;
	}


	/**
	 * 验证身份证号码
	 * @param idCard 居民身份证号码15位或18位，最后一位可能是数字或字母
	 * @return 验证成功返回true，验证失败返回false
	 */
	public static boolean isIdCard(String idCard) {
		boolean flag = false;
		try {
			Pattern p = Pattern.compile("[1-9]\\d{13,16}[a-zA-Z0-9]{1}");
			Matcher m = p.matcher(idCard);
			flag = m.matches();
		} catch(Exception e){
			flag = false;
		}
		return flag;
	}


	/**
	 * 是否是httpUrl网络资源地址
	 * @param url
	 * @return
	 */
	public static boolean isHttpUrl(String url) {
		if(url==null || url.equals("")) {
			return false;
		}

		String regex = "(http:|https:)//[^[A-Za-z0-9\\._\\?%&+\\-=/#]]*";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(url);

		if(matcher.find()) {
			return url.trim().equalsIgnoreCase(matcher.group());
		}
		else {
			return false;
		}
	}


	/**
	 * 传入一个字符串，自动识别是身份证、邮箱或是手机号，然后根据情况隐藏字符串，隐藏字符用'*'号代替
	 * @param str	传入字符串
	 * @return
	 */
	public static String hideString(String str) {
		if(!isNull(str)) {
			// 如果是邮箱
			if(isEmail(str)) {
				// 将开头第三个到@之前的字符隐藏掉
				return str.substring(0, 3) + "***" + str.substring(str.indexOf("@"));
			}
			else if(isMobileNO(str)) {
				// 将开头3个与后3个之前的数字隐藏掉
				return str.substring(0, 3) + "***" + str.substring(str.length()-3);
			}
			else if(isIdCard(str)) {
				// 除开头6位和倒数3、4位以外，全隐藏
				if(str.length()==15) {
					return str.substring(0, 6) + "***" + str.substring(12, 14) + "*";
				}
				else {
					return str.substring(0, 6) + "***" + str.substring(14, 16) + "**";
				}
			}
			else {
				return "******";
			}
		}
		else {
			return "";
		}
	}


	/**
	 * 传入一个字符串，判断是否为纯数字
	 * @param str	传入字符串
	 * @return
	 */
	public static boolean isNumber(String str) {
		boolean flag = false;
		try {
			Pattern p = Pattern.compile("[0-9]*");
			Matcher m = p.matcher(str);
			flag = m.matches();
		} catch(Exception e){
			flag = false;
		}
		return flag;
	}


	/**
	 * 传入一个字符串，判断是否为金额数字（小数点后2位）
	 */
	public static boolean isMoney(String str) {

		// 判断小数点后2位的数字的正则表达式
		Pattern pattern = Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$");
		Matcher match = pattern.matcher(str);
		return match.matches();

	}


	/**
	 * 删除文本中的Html标记
	 * @return
	 */
	public String delHTMLTag(String htmlStr) {

		String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
		String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
		String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式
		Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
		Matcher m_script=p_script.matcher(htmlStr);
		htmlStr=m_script.replaceAll("");//过滤script标签
		Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
		Matcher m_style=p_style.matcher(htmlStr);
		htmlStr=m_style.replaceAll(""); //过滤style标签
		Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
		Matcher m_html=p_html.matcher(htmlStr);
		htmlStr=m_html.replaceAll(""); //过滤html标签
		return htmlStr.trim(); //返回文本字符串

	}


	/**
	 * 截取字符串中的所有image路径
	 * @param str
	 * @return
	 */
	public List<String> imgCut(String str) {
		str = str.trim();
		String[] b = str.split("src=\"");

		List<String> list = new ArrayList<String>();

		for (int i = 1; i < b.length; i++) {
			String[] c = b[i].split("\"");
			list.add(c[0]);
		}

		return list;
	}


	/**
	 * 截取字符串中的所有src路径
	 * @param str
	 * @return
	 */
	public List<String> embCut(String str) {

		str = str.trim();
		String[] b = str.split("src=\"");

		List<String> list = new ArrayList<String>();

		for (int i = 1; i < b.length; i++) {
			String[] c = b[i].split("\"");
			if(c[0].startsWith("http:") &&c[0].contains(".swf")){
				if(c[0].indexOf("player.ku6") == -1 && c[0].indexOf("static.video.qq") == -1){
					list.add(c[0]);
				}
			}
		}

		return list;
	}


	/**
	 * 截取字符串中的所有image路径
	 * @param str
	 * @return
	 */
	public List<String> imgHtmlCut(String str) {
		str = str.trim();
		String[] b = str.split("<img\"");

		List<String> list = new ArrayList<String>();

		for (int i = 1; i < b.length; i++) {
			String[] c = b[i].split("\"");
			list.add(c[0]);
		}

		return list;
	}


	/**
	 * 转义html标签
	 * @param str html内容
	 * @return
	 */
	public static String escapeHTML(String str) {
		return StringUtils.replaceEach(str, new String[]{"&", "\"", "<", ">"}, new String[]{"&amp;", "&quot;", "&lt;", "&gt;"});
	}


	/**
	 * 过滤掉规格中的关键字
	 *
	 * @param prots_str		规格文字，格式如："颜色分类:豆绿;尺码:L",必须是':;'形式
	 * @param prot_name		过滤的规格名，颜色分类|尺寸|null，为null时过滤所有
	 * @param filter		过滤的条件
	 * 						filter的格式如：["偏大","(key_words)","【key_words】"]
	 * @return
	 */
	public static String filterProtsStr(String prots_str, String prot_name, String filter) {

		if (filter==null || filter.trim().length()==0) {
			return prots_str;
		}

		JSONArray filterArray = JSON.parseArray(filter);
		if (filterArray.size() == 0) {
			return prots_str;
		}

		String[] p1Array = prots_str.split(";");
		for (String p1 : p1Array) {
			if (prot_name!=null && p1.startsWith(prot_name)) {
				String[] p2Array = p1.split(":");
				for (Object str : filterArray) {
					if (str.equals("(key_words)")) {
						p2Array[1] = p2Array[1].replaceAll("\\([^)]*\\)", "");
					}
					else if (str.equals("【key_words】")) {
						p2Array[1] = p2Array[1].replaceAll("【[^】]*\\】", "");
					}
					else {
						p2Array[1] = p2Array[1].replaceAll(str.toString(), "");
					}
				}
				return prots_str.replace(p1, p2Array[0]+":"+p2Array[1]);
			}
			else if (prot_name==null && p1.startsWith("颜色分类")) {
				String[] p2Array = p1.split(":");
				for (Object str : filterArray) {
					if (str.equals("(key_words)")) {
						p2Array[1] = p2Array[1].replaceAll("\\([^)]*\\)", "");
					}
					else if (str.equals("【key_words】")) {
						p2Array[1] = p2Array[1].replaceAll("【[^】]*\\】", "");
					}
					else {
						p2Array[1] = p2Array[1].replaceAll(str.toString(), "");
					}
				}
				prots_str = prots_str.replace(p1, p2Array[0]+":"+p2Array[1]);
			}
			else if (prot_name==null && p1.startsWith("尺码")) {
				String[] p2Array = p1.split(":");
				for (Object str : filterArray) {
					if (str.equals("(key_words)")) {
						p2Array[1] = p2Array[1].replaceAll("\\([^)]*\\)", "");
					}
					else if (str.equals("【key_words】")) {
						p2Array[1] = p2Array[1].replaceAll("【[^】]*\\】", "");
					}
					else {
						p2Array[1] = p2Array[1].replaceAll(str.toString(), "");
					}
				}
				prots_str = prots_str.replace(p1, p2Array[0]+":"+p2Array[1]);
			}
		}

		return prots_str;
	}


	/**
	 * 随机生成字符串含字母数字
	 * @param length
	 * @return
	 */
	public static String getRandomString(int length) { //length表示生成字符串的长度
		String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		Random random = new Random(new Date().getTime());
		StringBuffer sb = new StringBuffer();
		for (int i=0; i<length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}


	/**
	 * 随机生成数字字符串
	 * @param length
	 * @return
	 */
	public static String getRandomNumberString(int length) { //length表示生成字符串的长度
		String base = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
		Random random = new Random(new Date().getTime());
		StringBuffer sb = new StringBuffer();
		for (int i=0; i<length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}


	/**
	 * 将输入的数字转换成中文
	 * @param string
	 * @return
	 * @author Guojh
	 */
	public static String toChinese(String string) {
		String[] s1 = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
		String[] s2 = { "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千" };

		String result = "";

		int n = string.length();
		for (int i = 0; i < n; i++) {

			int num = string.charAt(i) - '0';

			if (i != n - 1 && num != 0) {
				result += s1[num] + s2[n - 2 - i];
			} else {
				result += s1[num];
			}
			//System.out.println("  "+result);
		}

		//System.out.println("----------------");
		//System.out.println(result);
		return result;

	}


	/** 查询数组中是否包含某一元素    Guojh  2017年2月9日 **/
	public static Boolean checkObjInArray(Object [] arr, Object str){

		boolean flag = false;

		if (null == str || null == arr) {
			return flag;
		}

		for(Object a : arr){

			if(str.equals(a)){
				flag = true;
				break;
			}

		}

		return flag;
	}


	/**
	 * 将浮点数格式de经纬度转换成度分秒的格式
	 * @author 	郭力珲
	 * @editor	韩晗
	 *
	 * @param 	jwd		单个经度或纬度坐标值，浮点数格式
	 * @return
	 */
	public static String transOneJWD2DFM(String jwd) {

		String dfm = "";
		BigDecimal c60 = new BigDecimal("60");

		// 度
		dfm = jwd.substring(0, jwd.indexOf(".")) + "°";

		// 度+分
		jwd = "0." + jwd.substring(jwd.indexOf(".")+1);
		jwd = new BigDecimal(jwd).multiply(c60).toString();
		dfm += jwd.substring(0, jwd.indexOf(".")) + "′";

		// 度+分+秒
		jwd = "0." + jwd.substring(jwd.indexOf(".")+1);
		dfm += new BigDecimal(jwd).multiply(c60).doubleValue() + "″";

		return dfm;

	}
	/**
	 * 将度分秒格式de经纬度转换成浮点数的格式
	 * @author 	郭力珲
	 * @editor	韩晗
	 *
	 * @param 	dfm			单个经度或纬度坐标值，度分秒格式
	 * @return
	 */
	public static String transOneDFM2JWD(String dfm) {

		BigDecimal jwd = BigDecimal.ZERO;
		BigDecimal c60 = new BigDecimal("60");
		BigDecimal c3600 = new BigDecimal("3600");

		String[] array = dfm.split("°|′|″");
		// 度
		jwd = jwd.add(new BigDecimal(array[0]));
		// 度+分
		jwd = jwd.add(new BigDecimal(array[1]).divide(c60, COORDINATE_SCALE, BigDecimal.ROUND_HALF_UP));
		// 度+分+秒
		jwd = jwd.add(new BigDecimal(array[2]).divide(c3600, COORDINATE_SCALE, BigDecimal.ROUND_HALF_UP));

		return jwd.doubleValue()+"";

	}
	/**
	 * 将字符串里浮点数格式经纬度转换成度分秒的格式
	 * @author 	郭力珲
	 * @editor	韩晗
	 *
	 * @param 	jwd		含有浮点数格式经纬度信息的字符串，通常是一对或者很多对经纬度
	 * @return
	 */
	public static String transJWD2DFM(String jwd) {
		StringBuffer sb = new StringBuffer();
		Pattern pattern = Pattern.compile("(\\d+\\.\\d*)");	// 匹配的模式
		Matcher match = pattern.matcher(jwd);
		while(match.find()) {
			match.appendReplacement(sb, transOneJWD2DFM(match.group(1)));
		}
		return sb.toString();
	}
	/**
	 * 将字符串里度分秒格式经纬度转换成浮点数的格式
	 * @author 	郭力珲
	 * @editor	韩晗
	 *
	 * @param 	jwd		含有度分秒格式经纬度信息的字符串，通常是一对或者很多对经纬度
	 * @return
	 */
	public static String transDFM2JWD(String dfm) {
		StringBuffer sb = new StringBuffer();
		Pattern pattern = Pattern.compile("(\\d+°\\d+′\\d{1,}[.]?[0-9]*″)");	// 匹配的模式
		Matcher match = pattern.matcher(dfm);
		while (match.find()) {
			match.appendReplacement(sb,  transOneDFM2JWD(match.group(1)));
		}
		return sb.toString();
	}


	/**
	 * 将字符串按JS算式执行，并返回计算结果
	 *
	 * @param 	js_formula	JS语法风格算式
	 */
	public static Object eval(String js_formula) {

		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("js");

		try {
			return engine.eval(js_formula);
		}
		catch (ScriptException e) {
			e.printStackTrace();
			return null;
		}

	}
	/**
	 * 将字符串按JS算式执行，并返回计算结果
	 *
	 * @param 	js_formula	JS语法风格算式
	 * @param 	objArray	算式传参，只支持“x,y,z”三个参数
	 */
	public static Object eval(String js_formula, Object... objArray) {

		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("js");

		String[] paramArray = "x,y,z".split(",");
		for(int i=0; i<objArray.length; i++) {
			engine.put(paramArray[i], objArray[i]);
			if (i >= 2) {
				break;
			}
		}

		try {
			return engine.eval(js_formula);
		}
		catch (ScriptException e) {
			e.printStackTrace();
			return null;
		}

	}


	/**
	 * 新增或格式化stringbuilder
	 * @param strBuilder
	 * @return
	 */
	public static StringBuilder newStringBuilderFun(StringBuilder strBuilder) {
		if (null == strBuilder) {
			strBuilder = new StringBuilder();
		}
		else {
			strBuilder.delete(0, strBuilder.length());
		}
		return strBuilder;
	}
	
}


